package com.xcity.game.adventure.model;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.adventure.AdventureService;
import com.xcity.game.adventure.AdventureType;
import com.xcity.game.io.SerializeStream;
import com.xcity.game.io.Serializer;
import com.xcity.game.io.SerializerUtils;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;

public class Adventure implements Serializer {

	@JSONField(name = "0")
	protected AdventureType lastAction; // 最近一次未成功执行的转盘动作，玩家重新上线后需处理（偷窃、攻击）
	@JSONField(name = "1")
	protected List<ActionTarget> lastActionTargets; // 该动作目标玩家
	@JSONField(name = "2")
	protected LinkedList<Foe> foes; // 仇人列表
	@JSONField(name = "3")
	protected ActionTarget nextStealTarget; // 下一次偷取目标
	@JSONField(name = "4")
	protected boolean doubled; // 最近一次动作是否为双倍奖励
	
	@JSONField(serialize = false)
	protected Player owner;
	
	public static final int FOES_MAX = 50;
	
	public Adventure() {}
	
	public Adventure(Player owner) {
		this.owner = owner;
	}
	
	public AdventureType getLastAction() {
		return lastAction;
	}

	public void setLastAction(AdventureType lastAction) {
		this.lastAction = lastAction;
	}
	
	public List<ActionTarget> getLastActionTargets() {
		return lastActionTargets;
	}

	public void setLastActionTargets(List<ActionTarget> lastActionTargets) {
		this.lastActionTargets = lastActionTargets;
	}

	public LinkedList<Foe> getFoes() {
		return foes;
	}

	public void setFoes(LinkedList<Foe> foes) {
		this.foes = foes;
	}

	public ActionTarget getNextStealTarget() {
		return nextStealTarget;
	}

	public void setNextStealTarget(ActionTarget nextStealTarget) {
		this.nextStealTarget = nextStealTarget;
	}
	
	public boolean isDoubled() {
		return doubled;
	}

	public void setDoubled(boolean doubled) {
		this.doubled = doubled;
	}

	public Player getOwner() {
		return owner;
	}

	public void setOwner(Player owner) {
		this.owner = owner;
	}
	
	public void addFoe(Foe foe) {
		if (foes == null) {
			foes = new LinkedList<Foe>();
		}
		Iterator<Foe> itr = foes.iterator();
		while (itr.hasNext()) {
			Foe f = itr.next();
			if (f.getId() == foe.getId()) {
				itr.remove();
				break;
			}
		}
		foes.addFirst(foe);
		if (foes.size() > FOES_MAX) {
			foes.removeLast();
		}
//		if (owner instanceof Actor) {
//			((Actor) owner).setDirty(true);
//		}
	}
	
	public Foe removeFoe(long id) {
		if (foes == null) {
			return null;
		}
		Iterator<Foe> itr = foes.iterator();
		while (itr.hasNext()) {
			Foe foe = itr.next();
			if (foe.getId() == id) {
				itr.remove();
				return foe;
			}
		}
		return null;
	}
	
	public void validate(AdventureService service) {
		PlayerCacheService pcs = App.getApp().getServiceManager().get(PlayerCacheService.class);
		if (nextStealTarget != null && pcs.find(nextStealTarget.getId()) == null) {
			// 合服可能没了
			setNextStealTarget(service.findNextStealTarget(owner));
		}
		AdventureType t = getLastAction();
		if (t == AdventureType.ATTACK || t == AdventureType.STEAL || t == AdventureType.CAPTURE) {
			List<ActionTarget> __targets__ = getLastActionTargets();
			for (int i = 0; i < __targets__.size(); i++) {
				if (pcs.find(__targets__.get(i).getId()) == null) {
					setLastAction(null);
					setLastActionTargets(null);
					break;
				}
			}
		}
	}
	
	@JSONField(serialize = false)
	public ActionTarget getAttackTarget() {
		if (lastAction == AdventureType.ATTACK && lastActionTargets != null && lastActionTargets.size() > 0) {
			return lastActionTargets.get(0);
		}
		return null;
	}

	@Override
	public void write(SerializeStream out) {
		SerializerUtils.serialize(this, out);
	}
	
}
